package com.eliansun.crawler.downloader.phantomjsdriver;

import org.apache.log4j.Logger;
import org.openqa.selenium.*;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.Downloader;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.PlainText;
import us.codecraft.webmagic.utils.UrlUtils;

import java.io.Closeable;
import java.io.IOException;
import java.util.Map;

/**
 * 2017/2/21.
 * 基于phantomjs的下载器
 * @author xpq
 */
@SuppressWarnings("deprecation")
public class PhantomjsDownloader implements Downloader,Closeable{

    private Logger logger = Logger.getLogger(getClass());

    private volatile WebDriverPool webDriverPool;

    private int threadnum = 0;

    private int count = 0;

    @Override
    public Page download(Request request, Task task) {
        init();
        Page page = null;
        try {
            WebDriver webDriver = webDriverPool.getWebDriver();
            webDriver.get(request.getUrl());
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            WebDriver.Options manage = webDriver.manage();
            Site site = task.getSite();
            if (site.getCookies() != null){
                for (Map.Entry<String,String> entry : site.getCookies()
                        .entrySet()) {
                    Cookie cookie = new Cookie(entry.getKey(),
                            entry.getValue());
                    manage.addCookie(cookie);
                }
            }
            Thread.sleep(2500);
            ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 10000)");//滚动条一致底部
            Thread.sleep(2500); //等待js加载
            ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 10000)");//防止懒加
            Thread.sleep(100);
            WebElement webElement = webDriver.findElement(By.xpath("/html"));
            String content = webElement.getAttribute("outerHTML");
            page = new Page();
            page.setRawText(content);
            page.setHtml(new Html(UrlUtils.fixAllRelativeHrefs(content,
                    request.getUrl())));
            page.setUrl(new PlainText(request.getUrl()));
            page.setRequest(request);
            webDriverPool.removeDriver(webDriver);
            webDriver.quit();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return page;
    }

    @Override
    public void setThread(int threadNum) {
        this.threadnum = threadnum;
    }

    public void setThreadnum(int threadnum) {
        this.threadnum = threadnum;
    }

    private void init(){
        if(webDriverPool == null){
            synchronized (this){
                webDriverPool = new WebDriverPool();
            }
        }
    }


    @Override
    public void close() throws IOException {
        Runtime rt = Runtime.getRuntime();
        rt.exec("taskkill /f /im phantomjs.exe");//杀掉phantomjs进程
        webDriverPool.closeAll();
    }
}
